package dao.impl;

import dao.CourierDao;
import pojo.entity.Courier;
import util.GetResultSet;
import util.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

public class CourierDaoImpl implements CourierDao {
    @Override
    public boolean insert(Courier c) {
        String sql = "insert into courier values(null,?,?,?,?,?,?,?,?,?,?,?,?)";
        Object[] params = new Object[]{c.getCourierStationName(),c.getCourierStationImage(),c.getCourierStationProvince()
        ,c.getCourierStationCity(),c.getCourierStationDistrict(),c.getCourierStationAddress(),c.getCourierStationStatus()
        ,c.getAddTime(),c.getCourierStationSort(),c.getCourierStationCommunityId(),c.getCourierStationCommunityName()
        ,c.getCourierStationOrderCount()};
        System.out.println("Courier插入:"+sql);
        System.out.println("插入数据"+ Arrays.toString(params));
        int i = JDBCDruidUtil.update(sql, params);
        JDBCDruidUtil.close();
        return i > 0;
    }

    @Override
    public boolean update(Courier c) {
        String sql = "update  courier set courierStationName = ?, `courierStationImage` = ?,"+
        "courierStationProvince = ?, `courierStationCity` = ?, `courierStationDistrict` = ?,"+
        "`courierStationAddress` = ?, `courierStationStatus` = ?, `addTime` = ?, `courierStationSort` = ?,"+
        "`courierStationCommunityId` = ?, `courierStationCommunityName` = ?,"+
        "`courierStationOrderCount` = ? WHERE courierStationId = ? ";
        Object[] params = new Object[]{c.getCourierStationName(),c.getCourierStationImage(),c.getCourierStationProvince()
                ,c.getCourierStationCity(),c.getCourierStationDistrict(),c.getCourierStationAddress(),c.getCourierStationStatus()
                ,c.getAddTime(),c.getCourierStationSort(),c.getCourierStationCommunityId(),c.getCourierStationCommunityName()
                ,c.getCourierStationOrderCount(),c.getCourierStationId()};
        System.out.println("Courier更新："+sql);
        System.out.println("插入数据："+ Arrays.toString(params));
        int i = JDBCDruidUtil.update(sql, params);
        JDBCDruidUtil.close();
        return i > 0;
    }

    @Override
    public boolean modifyStation(Courier courier) {
        String sql = "update  courier set courierStationStatus = ? where courierStationId = ? ";
        Object[] params = new Object[]{courier.getCourierStationStatus(),courier.getCourierStationId()};
        System.out.println("Courier修改状态:"+sql);
        System.out.println("插入数据："+ Arrays.toString(params));
        int i = JDBCDruidUtil.update(sql, params);
        JDBCDruidUtil.close();
        return i > 0;
    }

    @Override
    public boolean delete(int id) {
        String sql = "delete from courier where courierStationId = ?";
        Object[] params = new Object[]{id};
        System.out.println("Courier删除："+sql);
        System.out.println("插入数据："+ Arrays.toString(params));
        int i = JDBCDruidUtil.update(sql, params);
        return i > 0;
    }

//  courierStationName:
//  communityName:
//  pageNum: 1
//  pageSize: 5
    @Override
    public List<Courier> selectAll(Map<String,Object> map) {
        List<Courier> list = new ArrayList<>();
        List<Object> params = new ArrayList<>();
        StringBuilder sql = new StringBuilder(" select * from courier where 1=1 ");
        Object courierStationName = map.get("courierStationName");
        Object communityName = map.get("communityName");
        Object startTime =  map.get("startTime");
        Object pageNum = map.get("pageNum");

        if(courierStationName != null && !courierStationName.toString().isEmpty()){
            sql.append(" and courierStationName like ? ");
            params.add("%"+courierStationName+"%");
        }
        if(communityName!= null && !communityName.toString().isEmpty()){
            sql.append(" and courierStationCommunityName like ? ");
            params.add("%"+communityName+"%");
        }
        if(startTime != null && !startTime.toString().isEmpty()){
            sql.append(" and addTime between ? and ? ");
            params.add(startTime);
            params.add(map.get("endTime"));
        }
        if(pageNum!= null && !pageNum.toString().isEmpty()){
            sql.append(" limit ?,? ");
            int pn = Integer.parseInt(pageNum.toString());
            int ps = Integer.parseInt(map.get("pageSize").toString());
            params.add((pn-1)*ps);
            params.add(ps);
        }
        System.out.println("Courier列表:"+sql);
        System.out.println("插入数据："+params);
        ResultSet set = JDBCDruidUtil.query(sql.toString(), params.toArray());
        if(set != null){
            try{
                while (set.next()){
                    GetResultSet<Courier> get = new GetResultSet<>();
                    Courier courier = get.get(Courier.class, set);
                    list.add(courier);
                }
                JDBCDruidUtil.close();
                return list;
            }catch(SQLException e){
                e.printStackTrace();
            }finally {
                JDBCDruidUtil.close();
            }
        }
        return list;
    }

    @Override
    public Integer selectCount(Map<String,Object> map) {
        List<Object> params = new ArrayList<>();
        StringBuilder sql = new StringBuilder(" select count(*) from courier where 1=1 ");
        String courierStationName = map.get("courierStationName").toString();
        if(courierStationName != null && !courierStationName.isEmpty()){
            sql.append(" and courierStationName like ? ");
            params.add("%"+courierStationName+"%");
        }
        String communityName = map.get("communityName").toString();
        if(communityName!= null && !communityName.isEmpty()){
            sql.append(" and courierStationCommunityName like ? ");
            params.add("%"+communityName+"%");
        }
        String startTime = (String) map.get("startTime");
        if(startTime != null && !startTime.isEmpty()){
            sql.append(" and addTime between ? and ? ");
            params.add(startTime);
            params.add(map.get("endTime"));
        }
        System.out.println("Courier总数:"+sql);
        ResultSet set = JDBCDruidUtil.query(sql.toString(), params.toArray());
        if(set != null){
            try{
                while (set.next()){
                    int count = set.getInt(1);
                    JDBCDruidUtil.close();
                    return count;
                }
            }catch(SQLException e){
                e.printStackTrace();
            }finally {
                JDBCDruidUtil.close();
            }
        }
        JDBCDruidUtil.close();
        return 0;
    }
}
